import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import service from '@dfeidao/atom-web/msg/service';
import error from '@dfeidao/atom-web/msgbox/error';
import success from '@dfeidao/atom-web/msgbox/success';
import remove from '@dfeidao/atom-web/storage/clear';
import get from '@dfeidao/atom-web/storage/get';
import direct_to from '@dfeidao/atom-web/url/direct-to';


export default async function a003(fd: IFeidaoAiBrowserComponent) {
	// todo
	// 原密码
	const captcha = fd.data.node.querySelector<HTMLInputElement>('[ymm]').value;
	// 新密码
	const xmm = fd.data.node.querySelector<HTMLInputElement>('[xmm]').value;
	// 确定密码
	const password = fd.data.node.querySelector<HTMLInputElement>('[qrmm]').value;

	if (!captcha || !xmm || !password) {
		error('密码不能为空!');
		return;
	}

	// const user_pwd_regx = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
	// if (!user_pwd_regx.test(xmm) || '') {
	// 	return error('您输入的密码不符合要求！');
	// }

	if (xmm !== password) {
		error('确认密码错误！');
		return;
	}
	if (captcha === xmm) {
		error('新旧密码不能一致！');
		return;
	}

	// loginservice.updatepwd 修改密码服务
	const user = get('user');
	const user_code = user.usercode;
	const productid = user.productid;
	const msg = {
		captcha,// 旧密码
		edit_type: 'normal', // 修改密码方式
		id: user_code,// 可以用用户名代替
		password,// 新密码
		productid
	};
	const s = await service('loginservice.updatepwd', msg) as { result: boolean; msg: string; };
	if (s.msg === '原始密码不正确') {
		error('旧密码不对');
		return;
	}
	if (s.result === true) {
		success('修改成功');
		remove();
		direct_to('./login.html');
		return;
	} else {
		error('修改失败');
		return;
	}
}
